package com.jbit.examSystem.service.impl;

import com.jbit.examSystem.dao.AdminDao;
import com.jbit.examSystem.dao.StudentDao;
import com.jbit.examSystem.dao.TeacherDao;
import com.jbit.examSystem.exception.LoginErrorException;
import com.jbit.examSystem.exception.RegisterException;
import com.jbit.examSystem.pojo.*;
import com.jbit.examSystem.dto.UserDto;
import com.jbit.examSystem.pojo.Student;
import com.jbit.examSystem.service.StudentService;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private AdminDao adminDao;

    @Override
    public void register(UserDto userDto) throws RegisterException {
        //根据邮箱获取学员信息
        StudentInfo studentInfo = studentDao.getStudentByEmail(userDto.getEmail());
        if (studentInfo == null){
            throw new RegisterException("您不是北大青鸟学员，无法注册");
        }

        Student student = createStudent(studentInfo, userDto);

        studentDao.add(student);
    }

    private Student createStudent(StudentInfo studentInfo, UserDto userDto) {
        Student student = new Student();
        student.setId(studentInfo.getId());
        student.setClassId(studentInfo.getClassId());
        student.setBirthday(studentInfo.getBirthday());
        student.setEmail(studentInfo.getEmail());
        student.setSex(studentInfo.getSex());

        student.setPassword(userDto.getPassword());
        student.setName(userDto.getName());
        student.setPhone(userDto.getPhone());
        student.setIdentityId(userDto.getIdentityId());

        student.setImagePath("http://localhost/head-portrait/default.jpg");
        return student;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Student login(UserDto userDto) throws LoginErrorException {
        String name = userDto.getName();
        String password = userDto.getPassword();

        Student student = studentDao.loginByNameAndPassword(name,password);
        if (student == null) {
            throw new LoginErrorException("登录失败，用户名或密码错误!");
        }
        return student;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Student checkEmail(String receiveMailAccount) {
        //判断邮箱是否已被注册
        Student stu = studentDao.checkEmail(receiveMailAccount);
        return stu;
    }

    @Override
    public void setLoginTime(Date loginTime,Integer id,Date lastLoginTime) throws LoginErrorException {
        studentDao.setLoginTime(loginTime,id,lastLoginTime);
    }

    @Override
    public void updateInfo(Student student) {
        studentDao.updateInfo(student);
    }

    @Override
    public void updatePwd(User user) {
        switch (user.getIdentityId()){
            case 10000:
                studentDao.updatePwd(user);
                break;
            case 10001:
                teacherDao.updatePwd(user);
                break;
            case 10002:
                adminDao.updatePwd(user);
                break;
        }

    }

    @Override
    public Boolean verify(Integer identityId,String name,String oldPwd) {
        switch (identityId){
            case 10000:
                Student stu = studentDao.getPwdByName(name);
                if(oldPwd.equals(stu.getPassword())){
                    return true;
                }
                break;
            case 10001:
                Teacher teacher = teacherDao.getByName(name);
                if(oldPwd.equals(teacher.getPassword())){
                    return true;
                }
                break;
            case 10002:
                Administrator admin = adminDao.getByName(name);
                if(oldPwd.equals(admin.getPassword())){
                    return true;
                }
                break;
        }
        return false;
    }

    @Override
    public Student findById(Integer id) {
       return studentDao.selectById(id);
    }

    @Override
    public List<User> getAllStudent(String userName) {
        List<User> users = studentDao.getAllStudent(userName);
        return users;
    }
}